package service.impl;

import dao.ArticleDao;
import model.Article;
import pojo.PageEntity;
import service.ArticleService;
import utils.PageUtil;

import java.util.Date;
import java.util.List;


public class ArticleServiceImpl implements ArticleService {

    private ArticleDao articleDao = new ArticleDao(Article.class);


    public PageEntity<Article> findPage(Integer currPage, String title) {
        PageEntity<Article> pageModel = new PageEntity<>();
        Integer pageSize = pageModel.getPageSize();
        List<Article> data = articleDao.findPage(title, (currPage - 1) * pageSize, pageSize);
        Integer totalCount = countByTitle(title);
        pageModel = PageUtil.createArtPage(currPage,data,totalCount);
        return pageModel;
    }

    @Override
    public Integer countByTitle(String title) {
        return articleDao.countByTitle(title);
    }

    @Override
    public void add(Article article) {
        //默认浏览量为0
        article.setBrowseCount(0);
        //当前时间发布
        article.setPublishDate(new Date());
        articleDao.save(article);
    }

    @Override
    public Article findById(Integer id) {
        return articleDao.findById(id);
    }

    @Override
    public int update(Article article) {
        return articleDao.update(article);
    }


}
